package com.demo.utils;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.springframework.core.io.ClassPathResource;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;
/**
 * Created by byte2 on 2017-03-29.
 */
public class PdfHelper {

    public static ITextRenderer getRender() throws DocumentException, IOException {

        ITextRenderer render = new ITextRenderer();
//        ClassPathResource resource = new ClassPathResource("fonts/simsun.ttc");
        ClassPathResource resourcef = new ClassPathResource("fonts/simsun.ttf");
        Map<Object, Object> o=new HashMap<Object, Object>();
//        System.out.println(resource.getURL().toString().replace("tpl.ftl",""));
//        String path = getPath();
        //添加字体，以支持中文
//        render.getFontResolver().addFont(path + "fonts/ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//        render.getFontResolver().addFont(resource.getURL().toString(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        String path = resourcef.getURL().toString();
        render.getFontResolver().addFont(path.replace("file:"+(path.indexOf("usr/")>-1?"":"/"),""), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        return render;
    }

    //获取要写入PDF的内容
    public static String getPdfContent(String ftlPath, String ftlName, Object o) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
        return useTemplate(ftlPath, ftlName, o);
    }

    //使用freemarker得到html内容
    public static String useTemplate(String ftlPath, String ftlName, Object o) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {

        String html = null;

        Template tpl = getFreemarkerConfig(ftlPath).getTemplate(ftlName);
        tpl.setEncoding("UTF-8");

        StringWriter writer = new StringWriter();
        tpl.process(o, writer);
        writer.flush();
        html = writer.toString();
        return html;
    }

    /**
     * 获取Freemarker配置
     * @param templatePath
     * @return
     * @throws IOException
     */
    private static Configuration getFreemarkerConfig(String templatePath) throws IOException {
        Configuration config = new Configuration();
        File file = new File(templatePath);
        config.setDirectoryForTemplateLoading(file);
//        config.setDirectoryForTemplateLoading(new File("H:\\java\\marketingplatform\\target\\marketingplatform\\WEB-INF\\classes"));
        config.setEncoding(Locale.CHINA, "utf-8");
        return config;
    }
    /**
     * 获取类路径
     * @return
     */
    public static String getPath(){
        return PdfHelper.class.getResource("").getPath().substring(1);
    }


    public static void main(String[] arg) throws IOException {
        File fi = new File("H:/java/marketingplatform/target/marketingplatform/WEB-INF/classes");
        System.out.print(fi.getCanonicalPath());
    }
}
